package com.github.baichuan.application.scaffold.rest.jpa.organization;

import com.github.baichuan.application.scaffold.rest.domain.organization.Org;
import com.github.baichuan.application.scaffold.rest.domain.application.Application;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface OrgRepository extends JpaRepository<Org, String> {

    @Query("select org from Org org where org.application=:application and org.uid=:uid")
    Optional<Org> find(@Param("application") Application application, @Param("uid") String uid);

    @Query("select org from Org org where application=:application and parent=:parent and name=:name")
    Optional<Org> find(@Param("application") Application application, @Param("parent") Org parent, @Param("name") String name);

    @Query("select org from Org org where parent=:parent")
    List<Org> findChildren(@Param("parent") Org parent);

//    @Query(value = "select * from org org where org.branch LIKE CONCAT(:branch,'%') and org.branch!=:branch" , nativeQuery = true)
//    List<Org> findChildren(@Param("branch") String branch);

//    List<Org> findByApplication(Application application);

//    @Modifying
//    @Query(value = "update org set branch =:branch where uid=:uid" , nativeQuery = true)
//    void updateBranch (@Param("branch") String branch, @Param("uid") String uid);
}
